﻿// (c) Comkarl Calipso
// By Matías Fidemraizer (http://www.matiasfidemraizer.com)
// -------------------------------------------------
// http://calipso.codeplex.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

(function ($) {
    $namespace.using("comkarl.web", function (web) {

        /// <summary>Represents a set of operations, events and properties which configures client-side ScriptControls and gives access to them</summary>
        web.declareClass("PageManager", {
            ctor: function () {
                this._.registeredControls = [];
                this._.controls = [];

                this.ready.addEventListener(this.onPageManagerReady);

                var that = this;

                $().ready(
                    function () {
                        that.init.raise({ args: {}, $this: that });
                        that.ready.raise({ args: {}, $this: that });
                    }
                );
            },
            members: {
                events: ["init", "ready", "controlInit", "controlReady"],

                /// <summary>Gets registered controls.</summary>
                get registeredControls() {
                    return this._.registeredControls;
                },

                /// <summary>Gets loaded controls.</summary>
                get controls() {
                    return this._.controls;
                },

                /// <summary>Registers a control</summary>
                /// <parameter name="uniqueId">A unique identifier in order to uniquely identify this control within this manager</parameter>
                /// <parameter name="controlType">The type of the control (formerly the class)</parameter>
                registerControl: function (uniqueId, controlType) {
                    this.registeredControls.push({ uniqueId: uniqueId, controlType: controlType });
                },

                /// <summary>Initializes all registered controls</summary>
                initializeControls: function () {
                    var tempControl;
                    var tempUniqueId;

                    for (var controlIndex in this.registeredControls) {
                        tempUniqueId = this.registeredControls[controlIndex].uniqueId;

                        tempControl = new this.registeredControls[controlIndex].controlType;
                        this.controls[tempUniqueId] = tempControl;

                        tempControl.id = tempUniqueId;

                        this.controlInit.raise({ args: { control: tempControl } });

                        tempControl.derived.render();
                        tempControl.derived.initialize();

                        this.controlReady.raise({ args: { control: tempControl } });
                    }
                },

                onPageManagerReady: function () {
                    this.initializeControls();
                }
            }
        });
    });
})(jQuery);